package com.adminlab.print
{
	import com.adminlab.vos.Exam;
	import com.adminlab.vos.Report;
	import com.adminlab.vos.Result;
	
	import mx.collections.ArrayCollection;
	import mx.core.FlexGlobals;
	import mx.printing.FlexPrintJob;

	public class FormPrintManager
	{
		[Bindable]
		private var printingLines:ArrayCollection = new ArrayCollection();
		
		public function FormPrintManager()
		{
		}
		
		private function createPrintingLines( exams:ArrayCollection ):void
		{
			printingLines.removeAll();
			
			for each( var exam:Exam in exams )
			{
				// print exam header...
				printingLines.addItem( exam );
				
				for each( var result:Result in exam.examType.results )
				{
					// print result...
					printingLines.addItem( result ); 
					
					// print references...
					if( result.referenceDescription != ""  )
					{ 
						var references:Array = result.referenceDescription.split( "\n" );
						if( references.length > 0 )
						{
							for( var lines:int = 0; lines < references.length; lines++ )
							{
								printingLines.addItem( references[ lines ] );
							}
						}	  
					}
					
					
				}
			}
			
		}
		
		// The function to print the output.
		public function doPrint( report:Report ):void {
			var printJob:FlexPrintJob = new FlexPrintJob();
			if (printJob.start()) {
				// Create a FormPrintView control as a child of the current view.
				var thePrintView:FormPrintView = new FormPrintView();
				FlexGlobals.topLevelApplication.addElement(thePrintView);
				
				//Set the print view properties.
				thePrintView.width=printJob.pageWidth;
				thePrintView.height=printJob.pageHeight;
				// Set the data provider of the FormPrintView component's data grid
				// to be the data provider of the displayed data grid.
				createPrintingLines( report.exams ); 
				thePrintView.myDataGrid.dataProvider = printingLines; 
				thePrintView.header.data = report;
				// Create a single-page image.
				thePrintView.showPage("single");
				// If the print image's data grid can hold all the provider's rows,
				// add the page to the print job.
				if (!thePrintView.myDataGrid.validNextPage)  {
					printJob.addObject(thePrintView);
				}
					// Otherwise, the job requires multiple pages.
				else {
					// Create the first page and add it to the print job.
					thePrintView.showPage("first");
					printJob.addObject(thePrintView);
					thePrintView.pageNumber++;
					// Loop through the following code until all pages are queued.
					while (true) {
						// Move the next page of data to the top of the print grid.
						thePrintView.myDataGrid.nextPage();
						thePrintView.showPage("last");
						// If the page holds the remaining data, or if the last page
						// was completely filled by the last grid data, queue it for printing.
						// Test if there is data for another PrintDataGrid page.
						if (!thePrintView.myDataGrid.validNextPage) {
							// This is the last page; queue it and exit the print loop.
							printJob.addObject(thePrintView);
							break;
						} else {
							// This is not the last page. Queue a middle page.
							thePrintView.showPage("middle");
							printJob.addObject(thePrintView);
							thePrintView.pageNumber++;
						}
					}
				}
				// All pages are queued; remove the FormPrintView control to free memory.
				FlexGlobals.topLevelApplication.removeElement(thePrintView);
			}
			// Send the job to the printer.
			printJob.send();
		}
	}
}